import React from 'react';
import './index.scss';
import { isEmpty } from 'utils/index';
import { TRADE_SORT_BY } from 'tradePublic/consts';
import { getTradeSearch } from 'tradePublic/tradeDataCenter/api/searchListGet';
import { getTradeList } from 'tradePublic/tradeDataCenter/api/baseListGet';
import { fillFullinfo, getLogisticsInfo } from 'pages/tradeManagement/tradeList/service';
import LogisticsInfoList from './list';
import { handleLogisticsData } from '../../service/logisticsServices/handleLogisticsData';
import FooterPageChange from './retrieval/footer';
import HeaderTimePicker from './retrieval/header';

const InitPageInfo = {
    INIT_PAGE_NO: 1,
    INIT_PAGE_SIZE: 20,
};

/**
 * @Description: 物流管理页面
 * @author DCD
 * @date 2021/3/26
*/
class LogisticsManagement extends React.Component {
    constructor (props) {
        super(props);
        this.state = {
            dataSource: [],
            total: 0,
            pageNo: InitPageInfo.INIT_PAGE_NO,
            pageSize: InitPageInfo.INIT_PAGE_SIZE,
        };
        this.searchArgs = {};
    }

    /**
     * @Description: 加载订单
     * @author DCD
     * @date 2021/3/25
    */
    loadListData = async ({ pageNo = InitPageInfo.INIT_PAGE_NO, pageSize = InitPageInfo.INIT_PAGE_SIZE }) => {
        let res;
        // SELLER_CONSIGNED_PART
        if (!isEmpty(this.searchArgs)) {
            this.searchArgs.status = 'WAIT_BUYER_CONFIRM_GOODS';
            this.searchArgs.timeFilterBy = 'create_time';
            // 如果有搜索参数
            res = await getTradeSearch({
                pageSize,
                pageNo,
                searchArgs: this.searchArgs,
            });
        } else {
            res = await getTradeList({
                status: 'WAIT_BUYER_CONFIRM_GOODS',
                pageSize,
                pageNo,
                sortBy: TRADE_SORT_BY.create_time_desc.key,
            });
        }
        if (res.source == 'top') {
            await fillFullinfo(res.trades);
        }
        // 加载物流信息
        await getLogisticsInfo(res.trades);
        const handleData = handleLogisticsData(res.trades);
        this.setState({
            dataSource: handleData,
            total: res.totalResults,
        });
    }

    componentDidMount () {
        const { pageNo, pageSize } = this.state;
        this.loadListData({ pageNo, pageSize });
    }
    /**
     * @function handleRetrievalData 处理检索数据
     * @param {SearchCondition} value 检索数据
     */
    handleRetrievalData (value) {
        this.searchArgs = { ...value };
        const { pageNo, pageSize } = this.state;
        this.loadListData({ pageNo, pageSize });
    }
    /**
     * @function handleChangePage 处理分页数据变化
     * @param {{pageNo: number, pageSize: number}} value
     */
    handleChangePage (value) {
        this.setState({
            pageNo: value.pageNo,
            pageSize: value.pageSize,
        });
        this.loadListData({ pageNo: value.pageNo, pageSize: value.pageSize });
    }

    render () {
        const { dataSource, total, pageSize, pageNo } = this.state;
        return (
            <div className='logistics-management' >
                <HeaderTimePicker retrievalData={this.handleRetrievalData.bind(this)} />
                <LogisticsInfoList listData={dataSource} />
                <FooterPageChange pageNo={pageNo} pageSize={pageSize} changePage={this.handleChangePage.bind(this)} total={total} />
            </div>
        );
    }
}

export default LogisticsManagement;
